//
//  IDPViewController.m
//  Aestroid Game
//
//  Created by iD Student on 7/10/13.
//  Copyright (c) 2013 jasonkurtzman. All rights reserved.
//

#import "IDPViewController.h"

@interface IDPViewController ()

@end

@implementation IDPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    gameTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(updateGame) userInfo:nil repeats:YES];
    angle = 0;
    asteroidList = [[NSMutableArray alloc] init];
    [self spawnAsteroids];
	// Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)upButtonPressed:(id)sender
{
    upPressed = YES;
}

-(IBAction)downButtonPressed:(id)sender
{
    downPressed = YES;
}

-(IBAction)leftButtonPressed:(id)sender
{
    leftPressed = YES;
}

-(IBAction)rightButtonPressed:(id)sender
{
    rightPressed = YES;
}

-(IBAction)buttonReleased:(id)sender
{
    upPressed = NO;
    downPressed = NO;
    leftPressed = NO;
    rightPressed = NO;
}

-(void) moveAsteroids
{
    for(int i = 0; i < asteroidList.count; i++)
    {
        IDPAsteroid *ast = [asteroidList objectAtIndex:i];
        CGPoint oldCenter = ast.image.center;
        ast.image.center = CGPointMake(oldCenter.x + ast.velocityX, oldCenter.y + ast.velocityY);
    }
}

-(void) spawnAsteroids
{
    for(int i = 0; i < 5; i++)
    {
        IDPAsteroid *ast = [[IDPAsteroid alloc] init];
        [ast randomizeVelocity];
        [asteroidList addObject:ast];
        [self.view addSubview:ast.image];
    }
}

-(void) checkForMissileCollisions
{
    int i = 0;
    while(i < missileList.count)
    {
        IDPMissile *missile = [missileList objectAtIndex:i];
        int j = 0;
        CGRect missileRect = missile.image.frame;
        
        BOOL collided = NO;
        while (j < asteroidList.count)
        {
            IDPAsteroid *ast = [asteroidList objectAtIndex: j];
            CGRect astRect = ast.image.frame;
            if(CGRectIntersectsRect(missileRect, astRect))
            {
                score += ast.scoreValue;
                [asteroidList removeObject:ast];
                [missileList removeObject:missile];
                [ast.image removeFromSuperview];
                [missile.image removeFromSuperview];
                collided = YES;
                break;
            }else
            {
                j++;
            }
        }
        if(collided == NO)
        {
            i++;
        }
    }
}

-(void) updateMissiles
{
    [self checkForMissileCollisions];
    int i = 0;
    while (i < missileList.count)
    {
        IDPMissile *missile = [missileList objectAtIndex:i];
        
        CGPoint oldCenter = missile.image.center;
        if(oldCenter.x > 320 || oldCenter.x < 0 || oldCenter.y > 480 || oldCenter.y < 0)
        {
            [missile.image removeFromSuperview];
            [missileList removeObject:missile];
        }
        else
        {
            missile.image.center = CGPointMake(oldCenter.x + missile.velocityX, oldCenter.y + missile.velocityY);
            i++;
        }
    }
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    IDPMissile *newMissile = [[IDPMissile alloc] init];
    newMissile.image.center = spaceShip.center;
    newMissile.velocityX = 8 * cos(angle * 3.14 / 180);
    newMissile.velocityY = 8 * sin(angle * 3.14 / 180);
    newMissile.image.transform = CGAffineTransformMakeRotation(angle * 3.14 / 180);
    [missileList addObject:newMissile];
    [self.view addSubview:newMissile.image];
}

-(void) updateGame
{
    [self moveAsteroids];
    [self updateMissiles];
    if(upPressed)
    {
        spaceShip.center = CGPointMake(spaceShip.center.x + 5 * cos(angle * 3.14 / 180), spaceShip.center.y + 5 * sin(angle * 3.14 / 180));
    }else if(downPressed)
    {
        spaceShip.center = CGPointMake(spaceShip.center.x + -5 * cos(angle * 3.14 / 180), spaceShip.center.y + -5 * sin(angle * 3.14 / 180));
    }else if(rightPressed)
    {
        angle += 5;
        spaceShip.transform = CGAffineTransformMakeRotation(angle * 3.14 / 180);
    }else if(leftPressed)
    {
        angle -= 5;
        spaceShip.transform = CGAffineTransformMakeRotation(angle * 3.14 / 180);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
